home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / Book Chapters / 11 - Porting / Sample Code / ErrCheck.c < prev    next >
Text File  |  1995-03-28  |  4KB  |  154 lines

  1. /************************************************************************************
  2.  *                                                                                                                                                                    *
  3.  *    ErrCheck.c                                                                                                                                            *
  4.  *                                                                                                                                                                    *
  5.  *    ©1995 Douglas Grounds. All Rights Reserved.                                                                            *
  6.  *                                                                                                                                                                    *
  7.  *    This file contains error checking functions which makes checking for resource,    *
  8.  *    memory and other error conditions much simpler. Examples are given for the            *
  9.  *    use of each function.                                                                                                                        *
  10.  *                                                                                                                                                                    *
  11.  ************************************************************************************/
  12.  
  13.  #include <Dialogs.h>
  14.  #include <Memory.h>
  15.  #include <Resources.h>
  16.  #include <SegLoad.h>
  17.  #include <stdio.h>
  18.  #include <strings.h>
  19.  #include <Types.h>
  20.  
  21.  #ifndef TRUE
  22.  #define TRUE        1
  23.  #endif
  24.  
  25.  #ifndef FALSE
  26.  #define FALSE    0
  27.  #endif
  28.  
  29.  #include "ErrCheck.h"
  30.  
  31. /*-----------------------------------
  32.  *
  33.  *    Constants
  34.  *
  35.  *-----------------------------------*/
  36.  
  37.  #define        kErrAlertId                128
  38.  
  39. /*-----------------------------------
  40.  *
  41.  *    Example Usage
  42.  *
  43.  *-----------------------------------*/
  44.  
  45. #if (FALSE)
  46.  
  47.     void myExampleFunction (void)
  48.     {
  49.         Handle        h;
  50.         long            count;
  51.         short            err, myFRef;
  52.         
  53.         h = GetResource('DATA', 128);
  54.         if (checkResError(h))
  55.         {
  56.             notifyOfError("GetResource", "MyExampleFunction", ResError(), FALSE);
  57.             return;
  58.         }
  59.         
  60.         ... use "h" ...
  61.         
  62.         h = NewHandle( 1024 );
  63.         if (checkMemError(h))
  64.         {
  65.             notifyOfError("NewHandle", "MyExampleFunction", MemError(), FALSE);
  66.             return;
  67.         }
  68.         
  69.         ... use "h" ...
  70.         
  71.         ... assign myFRef, count
  72.         ... lock down "h"
  73.         
  74.         err = FSWrite(myFRef, &count, *h);
  75.         if (checkOSErr(err))
  76.         {
  77.             notifyOfError("FSWrite", "MyExampleFunction", err, FALSE);
  78.             return;
  79.         }
  80.     }
  81.  
  82. #endif
  83.  
  84.  
  85. /******************************************************
  86.  *    notifyOfError.                                                                        *
  87.  *                                                                                                        *
  88.  *    Displays an alert that informs the user that            *
  89.  *    a function has returned an error and where the        *
  90.  *    call to that function was made.                                        *
  91.  ******************************************************/
  92.  
  93. void notifyOfError (char *routineName, char *caller, short errValue, Boolean isFatal)
  94. {
  95.     Str255            localStr;
  96.     
  97.     // Prepare the text.
  98.     
  99.     sprintf((char *)localStr, "The function ‘%s’ (called by ‘%s’) returned an error. The error number was: %d.", 
  100.         routineName, caller, errValue);
  101.     c2pstr((char *)localStr);
  102.     
  103.     // Tell the user!
  104.     
  105.     ParamText(localStr, "\p", "\p", "\p");
  106.     (void) Alert(kErrAlertId, NULL);
  107.     
  108.     // If fatal error, then quit!
  109.     
  110.     if (isFatal)
  111.     {
  112.         // May need to perform shut-down functions
  113.         ExitToShell();
  114.     }
  115. }
  116.  
  117. /******************************************************
  118.  *    checkResError.                                                                        *
  119.  *                                                                                                        *
  120.  *    If either "h" is NULL, or ResError() != noErr,         *
  121.  *    return TRUE.                                                                            *
  122.  ******************************************************/
  123.  
  124. Boolean checkResError (Handle h)
  125. {
  126.     return ((h == NULL) || ResError());
  127. }
  128.  
  129. /******************************************************
  130.  *    checkOSErr.                                                                                *
  131.  *                                                                                                        *
  132.  *    Checks the value of "err" and returns TRUE if an    *
  133.  *    error has occurred. Could be implemented as a            *
  134.  *    macro.                                                                                        *
  135.  ******************************************************/
  136.  
  137. Boolean checkOSErr (OSErr err)
  138. {
  139.     return (err != noErr);
  140. }
  141.  
  142. /******************************************************
  143.  *    checkMemError.                                                                        *
  144.  *                                                                                                        *
  145.  *    Checks both the value of "myPointerOrHandle" and    *
  146.  *    the value of MemError. Returns TRUE if an error        *
  147.  *    has occurred.                                                                            *
  148.  ******************************************************/
  149.  
  150. Boolean checkMemError (void *myPointerOrHandle)
  151. {
  152.     return ((myPointerOrHandle == NULL) || MemError());
  153. }
  154.